<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Models\Set;
use App\Models\Agent;
use App\Models\Mem;

class AdminController extends Controller{
    
    public function __construct(Request $req){
        if($req->input('offset') > 0){
            $req->merge(["page"=>$req->input('offset') / $req->input('limit') + 1]);
        }
    }
    
    public function V($page,$params = array()){
        $action = explode('\\', Route::current()->getControllerClass());
        $action = strtolower($action[count($action) - 1]);
        echo view('admin.header',['action'=>$action]);
        return view("admin.{$action}.{$page}",$params);
    }
    
    public function login(Request $req){
        if ($req->method() == 'POST') {
            $username = $req->input('username','');
            $passwd = $req->input('passwd','');
            if ($username != 'admin') return wxresp(0,'账密不对');
            if ($passwd != 'superbar666') return wxresp(0,'账密不对');
            $token = md5('superbar666');
            Redis::setex('superbar_logintoken',30 * 24 * 3600,$token);
            return response(json_encode(array('code'=>1)))->cookie('superbar_logintoken',$token,30 * 24 * 3600);
        }
        return view("admin.login");
    }
    
    public function app(Request $req,$unicode = ''){
        $set = Set::getSet('wxpay');
        $iswx = strpos($_SERVER['HTTP_USER_AGENT']??'', 'MicroMessenger');
        if ($iswx) {
            $desturl = $req->url();
            if ($req->get('code')){
                $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$set['appid']}&secret={$set['appsecret']}&code={$req->get('code')}&grant_type=authorization_code";
                $res = curPost($url);
                if (!isset($res['openid'])) die('授权失败'.json_encode($res,JSON_UNESCAPED_UNICODE));
                if (!empty($unicode)) {
                    $aid = Agent::where('code',$unicode)->value('id');
                    $mem = Mem::where('aid',$aid)->where('openid',$res['openid'])->first();
                    if (empty($mem)) {
                        $mem = new Mem();
                        $mem->aid = $aid;
                        $mem->openid = $res['openid'];
                        $mem->createtime = time();
                        $mem->save();
                    }
                }
                return redirect($desturl)->cookie('superbar_memlastopenid',base64_encode($res['openid']),365*24*3600);
            }else if (!$req->hasCookie('superbar_memlastopenid')) {
                $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$set['appid']}&redirect_uri={$desturl}&response_type=code&scope=snsapi_base#wechat_redirect";
                return redirect($url);
            }
            $resp = response()->view("app.index",array('jstoken'=>(new \WxPay($set))->getJsTicket($req->fullUrl())));
            if ($unicode) $resp->cookie('superbar_unicode',base64_encode($unicode),365*24*3600);
            return $resp;
        }else{
            if (!isset($set['wxpay'])) die('请先完成初始化');
            return view("app.qr",array('url'=>"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".(new \WxPay($set))->getQrTicket()));
        }
    }
}